<template>
  <el-scrollbar style="height:500px">
    model
    <el-row :gutter="20">
      <el-col :span="7">
        表：<br>
        <textarea
          cols="20"
          rows="5"
          :value="JSON.stringify(modelTable, null, 2)"
        />
        字段：<br>
        <textarea
          cols="40"
          rows="15"
          :value="JSON.stringify(modelColumList, null, 2)"
        />
      </el-col>
      <el-col :span="17">
        model：<br>
        <div>
          CREATE TABLE `{{modelTable.tableName}}` ( <br>
            <div v-for="(item, key, index) in modelColumList" :key="item.columnId">
              <template v-if="index === 0">
                &nbsp;&nbsp;`{{item.colName}}` INT NOT NULL AUTO_INCREMENT COMMENT '{{item.content}}',
              </template>
              <template v-else>
                &nbsp;&nbsp;`{{item.colName}}` {{typeAndSize(item.colType, item.colSize)}} COMMENT '{{item.content}}' NOT NULL,
              </template>
            </div>
              &nbsp;&nbsp;`isDel` TINYINT NOT NULL DEFAULT 0 COMMENT '删除标记，1：删除；0：正常', <br>
              &nbsp;&nbsp;`addUserId` INT NOT NULL DEFAULT 0 COMMENT '添加人用户ID', <br>
              &nbsp;&nbsp;`addTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间', <br>
              &nbsp;&nbsp;`updateUserId` INT NOT NULL DEFAULT 0 COMMENT '最后修改人用户ID', <br>
              &nbsp;&nbsp;`updateTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修时间', <br>
            &nbsp;&nbsp;PRIMARY KEY (`{{getKeyName()}}`)) <br>
          ENGINE = InnoDB <br>
          DEFAULT CHARACTER SET = utf8mb4 <br>
          COLLATE = utf8mb4_0900_ai_ci <br>
          COMMENT = '{{modelTable.content}}'; <br>
    </div>
      </el-col>
    </el-row>
  </el-scrollbar>

</template>

<script>
import { ref, reactive, watch } from 'vue'
// eslint-disable-next-line import/no-absolute-path
import VueDS from '/nf-state'

</script>

<script setup>

// 状态
const { reg, get } = VueDS.useStore()
const helpDataBaseMeta = get.helpDataBaseMeta()

// 表的model
const modelTable = helpDataBaseMeta.tableMeta
// 字段的 model 数据列表
const modelColumList = helpDataBaseMeta.columnListMeta

// 获取主键字段名
const getKeyName = () => {
  const key = Object.keys(modelColumList)[0]
  if (typeof modelColumList[key] === 'undefined') {
    return ''
  } else {
    return modelColumList[key].colName
  }
}

// 根据类型和大小返回语句
const typeAndSize = (type, size) => {
  let re = ''
  switch (type.toUpperCase()) {
    // 正数
    case 'INT':
    case 'TINYINT':
    case 'SMALLINT':
    case 'MEDIUMINT':
    case 'BIGINT':
    // 小数
    case 'DECIMAL':
    case 'DOUBLE':
    case 'FLOAT':
    case 'REAL':
    // 日期和时间
    case 'DATE':
    case 'DATETIME':
    case 'TIME':
    case 'TIMESTAMP':
    case 'YEAR':
    // 大文本
    case 'LONGTEXT':
    case 'MEDIUMTEXT':
    case 'TEXT':
    case 'TINYTEXT':
    // 二进制和bool
    case 'BIT':
    case 'BOOLEAN':
    case 'ENUM':
    case 'SET':
      re = type.toUpperCase()
      break
    // 字符串
    case 'VARCHAR':
    case 'CHAR':
    case 'NCHAR':
    case 'NVARCHAR':
      re = type.toUpperCase() + '(' + size + ')'
      break
    case '对象':
    case '数组':
      re = 'JSON'
      break
  }
  return re
}
</script>
